package cn.kgc.tangcco.controller;

import cn.kgc.tangcco.model.FdfsParam;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @author 李昊哲
 * @Description
 * @create 2021/1/2 10:14
 */

@RestController
public class FileController {
    @Value("${fdfs_storage_proxy.protocol}")
    private String fdfs_storage_proxy_protocol;

    @Value("${fdfs_storage_proxy.domain}")
    private String fdfs_storage_proxy_domain;

    @Value("${fdfs_storage_proxy.port}")
    private String fdfs_storage_proxy_port;

    @Autowired
    private FdfsParam fdfsParam;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    @PostMapping(value = "/img")
    public ResponseText<Map<String, String>> img(@RequestParam(value = "file", required = false) MultipartFile file) {

        Set<MetaData> metaDataSet = new HashSet<>();
        metaDataSet.add(new MetaData("Author", "李昊哲"));
        metaDataSet.add(new MetaData("CreateDate", new Date().toString()));
        Map<String, String> map = new HashMap<>();
        try {
            String originalFilename = file.getOriginalFilename();
            StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(originalFilename), metaDataSet);
            String fullPath = storePath.getFullPath();
            // 存储服务器web服务地址
            String fdfs_storage_proxy_url = fdfs_storage_proxy_protocol + fdfs_storage_proxy_domain + ":" + fdfs_storage_proxy_port + "/";
            String thumbImagePath = thumbImageConfig.getThumbImagePath(fullPath);
            map.put("fullPath", fdfs_storage_proxy_url + storePath.getFullPath());
            map.put("thumbImagePath", fdfs_storage_proxy_url + thumbImagePath);
            return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), map);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseText<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), map);
        }
    }

    @PostMapping(value = "/upload")
    public ResponseText<FdfsParam> upload(@RequestParam(value = "file", required = false) MultipartFile file) {

        Set<MetaData> metaDataSet = new HashSet<>();
        try {
            String originalFilename = file.getOriginalFilename();
            StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(originalFilename), metaDataSet);
            String fullPath = storePath.getFullPath();
            // 存储服务器web服务地址
            String thumbImagePath = thumbImageConfig.getThumbImagePath(fullPath);
            fdfsParam.setFullPath(storePath.getFullPath());
            fdfsParam.setThumbImagePath(thumbImagePath);
            return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), fdfsParam);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseText<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), fdfsParam);
        }
    }
}
